<?php
	require_once( "inc_systemFunctions.php" );
	page_header( "Wayne Schlegel Inc. | Setup", true, "myflight" );
	
	include( "_setup_adminNavi.php" );

	$build_css = "";
	$build_js  = "";

?>

<?php
	if( !empty($_GET['pid'] ) ){
		if( $_GET['from'] == 0 ){
			$to = 1;
		} else {
			$to = 0;
		}
		$upd_sql = "UPDATE reviews SET isApproved=" . $to . " WHERE id=" . $_GET['rid'];
		$upd_res = mysql_query( $upd_sql );
		if( $upd_res ){
			message( "Approval changed!", "green" );
		} else {
			message( "Approval NOT changed!", "red" );
		}
	}
?>

<table class="iTable">
	<tr>
		<td>ID</td>
		<td>user</td>
		<td>product</td>
		<td>review</td>
		<td>approved</td>
	</tr>
<?php
	$sql = "SELECT * FROM reviews ORDER BY isApproved ASC, id DESC";
	$res = mysql_query( $sql );
	$cnt = 0;
	if( $res ){
		while( $row = mysql_fetch_array( $res ) ){
			$cnt++;
			$build_css .= "#_review" . $row['id'] . "{display: none;}";
			$build_js  .= '$(function(){$("#showReview' . $row['id'] . '").click(function(event){event.preventDefault();$("#_review' . $row['id'] . '").slideToggle();});$("#_review' . $row['id'] . ' a").click(function(event) {event.preventDefault();$("#_review' . $row['id'] . '").slideUp();});});';
			$currentUser    = getUserData( $row['userData_id'] );
			$currentProduct = getProduct( $row['product_id'] );
		?>
		<tr <?php if( !empty( $_GET['rid'] ) && $_GET['rid'] == $row['id'] ){ echo "class=\"newItem\""; } ?> >
			<td><?=$row['id']?></td>
			<td><?=$currentUser['firstName']?> <?=$currentUser['lastName']?></td>
			<td><?=$currentProduct['marketingName']?></td>
			<td width="65%"><a href="#" id="showReview<?=$row['id']?>"><?=$row['title']?> [+]</a><p id="_review<?=$row['id']?>"><?=$row['text']?></p></td>
			<td><a style="display: block;" href="_setup_approveReviews.php?pid=<?=$row['product_id']?>&rid=<?=$row['id']?>&from=<?=$row['isApproved']?>"><?=iconStatus( $row['isApproved'] );?></a></td>
		</tr>
		<?php
		}
	}
?>
	<tr class="lastItem">
		<td colspan="5">...</td>
	</tr>
</table>
<style type="text/css">
<?=$build_css?>
</style>

<script type="text/javascript">
<?=$build_js?>
</script>

<?php
	page_footer();
?>